package com.coocaa.x.service.litedb;

import android.content.Context;
import android.os.RemoteException;

import com.coocaa.x.framework.data.JObject;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litedb.aidl.ILiteDBService;
import com.coocaa.x.service.litedb.data.common.obj.KVMap;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by C0der on 2015/3/11.
 */
public class LiteDBServiceStub extends ILiteDBService.Stub {
    private LiteDBHelper dbHelper = null;
    private Context mContext = null;

    public LiteDBServiceStub(Context context) {
        dbHelper = new LiteDBHelper(context, "litedb.db");
        mContext = context;
    }

    @Override
    public boolean createTable(String tablename, String jsonMap, int version)
            throws RemoteException {
        KVMap kv = new KVMap(JObject.parseJObject(jsonMap, LinkedHashMap.class));
        return dbHelper.createTable(tablename, kv.toKeysArray(), kv.toValuesArray(), version);
    }


    @Override
    public void whoAmI() {
        LogUtils.d("wai", "<<LiteDBService>> who am i??  " + mContext.getPackageName());
    }

    @Override
    public boolean deleteTable(String tablename) throws RemoteException {
        return dbHelper.deleteTable(tablename);
    }

    @Override
    public boolean insert(String tablename, String jsonMap) throws RemoteException {
        KVMap kv = new KVMap(JObject.parseJObject(jsonMap, LinkedHashMap.class));
        return dbHelper.insert(tablename, kv.toKeysArray(), kv.toValuesArray());
    }

    @Override
    public boolean delete(String tablename, String whereClause, List<String> args)
            throws RemoteException {
        String[] _args = new String[args.size()];
        args.toArray(_args);
        if (whereClause.trim().equals(""))
            whereClause = null;
        return dbHelper.delete(tablename, whereClause, _args);
    }

    @Override
    public boolean update(String tablename, String jsonMap, String whereClause, List<String> args)
            throws RemoteException {
        KVMap kv = new KVMap(JObject.parseJObject(jsonMap, LinkedHashMap.class));
        String[] _args = new String[args.size()];
        args.toArray(_args);
        if (whereClause.trim().equals(""))
            whereClause = null;
        return dbHelper.update(tablename, kv.toContentValues(), whereClause, _args);
    }

    @Override
    public List<String> select(String tablename, List<String> fields, String whereClause,
                               List<String> args, String groupby, String having, String orderby)
            throws RemoteException {
        String[] _fields = new String[fields.size()];
        fields.toArray(_fields);
        String[] _args = new String[args.size()];
        args.toArray(_args);
        if (whereClause.trim().equals(""))
            whereClause = null;
        return dbHelper.select(tablename, _fields, whereClause, _args, groupby, having, orderby);
    }
}
